<template>
	<view class="jigou">
		<u-navbar :is-back="showBack" :border-bottom="false" title="选择机构" ref="navbar"></u-navbar>
		<view class="radio">
			<u-radio-group shape='square' wrap="true" v-model="value" @change="radioGroupChange">
				<u-radio 
					v-for="(item, index) in list" :key="index" 
					:name="item.id"
				>
					<view class="item line1">
						{{item.organ_name}}
					</view>
				</u-radio>
			</u-radio-group>
		</view>
		<view :style="'height: calc(120rpx + '+iosSafeBottom+'px + '+SafeBottom+'rpx);'"></view>
		<view class="footer" ref="footer" :style="'padding-bottom: calc('+iosSafeBottom+'px + '+SafeBottom+'rpx);box-sizing: content-box;'">
			<view class="btn" @click="submit">确认选择</view>
		</view>
	</view>
</template>

<script>
	const app = getApp()
	import login from '../../api/login.js'
	import common from '../../api/common.js'
	import request from '../../api/request.js'
	export default{
		data(){
			return{
				iosSafeBottom:app.globalData.iosSafeBottom,
				SafeBottom:app.globalData.iosSafeBottom==0?'24':'-24',
				list: app.globalData.userJiGou,
				value: app.globalData.userJiGou_id,
				showBack: true,
			}
		},
		onShow() {
			uni.hideKeyboard() // 收起键盘
		},
		onLoad(e) {
			if(e.showBack){
				this.showBack = e.showBack==0?false:true
			}
		},
		methods:{
			submit(){
				let jigou = app.globalData.userJiGou
				let f = jigou.find(e=>e.id == this.value)
				const self = this;
				if(app.globalData.isWxwork){
					// 如果为企业微信多机构登录，则添加一个绑定接口
					let params = {
						open_userid: app.globalData.open_userid,
						token: f.token
					}
					uni.request({
						url:app.globalData.baseUrl + '/qywechat/bindQyuser',
						data:params,
						method:"POST",
						header:{
							"Content-Type":"application/x-www-form-urlencoded"
						},
						success(e) {
							// 绑定成功或重复绑定都跳转
							if(e.data.code == 200){
								login.saveUserOpenId(app.globalData.open_userid)
							}else if(e.data.code == 429){
								login.saveUserOpenId(f.open_userid)
							}
							if(e.data.code == 200 || e.data.code == 429){
								login.saveUserInfo(f)
								login.saveUserJiGou(jigou,self.value)
								common.go('/pages/index/index',2)
							}
						}
					})
				}else{
					login.saveUserInfo(f)
					login.saveUserJiGou(jigou,this.value)
					common.go('/pages/index/index',2)
				}
			},
			radioGroupChange(e){
				console.log(e)	
			},
		}
	}
</script>

<style scoped>
	.jigou .radio{
		padding: 30rpx;
	}
	.jigou .radio .item{
		width: 600rpx;
		padding: 20rpx 0;
		border-bottom: 1px solid #ebedf0;
	}
	.jigou .footer{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 120rpx;
		background-color: #FFFFFF;
		z-index: 999;
	}
	.jigou .footer .btn{
		width: 622rpx;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		margin: 24rpx auto 0;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #ffffff;
		z-index: 99;
		background-color: #1661FF;
	}
</style>
